﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Diagnostics;

using JigsawService.DataAccess.Usage;
using JigsawService.Parsers;
using JigsawService.Utility;

namespace JigsawService.Utility
{
    public class ErrorUtility : JigsawBase
    {

        public ErrorUtility()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="ex"></param>
        public void LogClientException(string errmsg, APIMessage apiMsg)
        {
            // log error to the database only
            WriteToDatabase("Client", errmsg, apiMsg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="apiMsg"></param>
        public void LogServerException(Exception ex, APIMessage apiMsg)
        {
            // log error to the eventviewer and database
            LogUtility lutil = new LogUtility();
            string errmsg = lutil.GetExceptionInfo(ex);
            lutil.WriteToEventLog(errmsg);
            WriteToDatabase("Server", errmsg, apiMsg);
            SetExceptionResult(ex.Message, apiMsg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errmsg"></param>
        /// <param name="apiMsg"></param>
        public void LogServerException(string errmsg, APIMessage apiMsg)
        {
            // log error to the eventviewer and database
            LogUtility lutil = new LogUtility();
            lutil.WriteToEventLog(errmsg);
            WriteToDatabase("Server", errmsg, apiMsg);
            SetExceptionResult(errmsg, apiMsg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errmsg"></param>
        /// <param name="sutil"></param>
        public void WriteToDatabase(string errsource, string errormsg, APIMessage apiMsg)
        {
            ErrorLogDA err = new ErrorLogDA(apiMsg);
            err.SaveError(errsource, errormsg, apiMsg);
        }

    }
}